package com.aisway.faceswap.utils

import android.app.Activity
import android.text.TextUtils
import java.util.concurrent.CopyOnWriteArrayList


/**
Author: shao_g
Date: 2021年4月22日16:46:35
 */
open class ActivityManager {
    /**
     * 添加Activity到堆栈
     */
    fun addActivity(activity: Activity) {
        if (activityStack == null) {
            activityStack = CopyOnWriteArrayList()
        }
        activityStack!!.add(activity)
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivity(): Activity? {
        return if (activityStack == null) {
            null
        } else activityStack!![activityStack!!.size - 1]
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    fun finishCurrentActiviy() {
        if (activityStack == null) {
            return
        }
        val activity = activityStack!![activityStack!!.size - 1]
        finishActivity(activity)
    }
    /**
     * 结束除指定Activity外的所有Activity
     */
    fun finishAllActivitiesExcept(activityClassToKeep: Class<*>) {
        val iterator = activityStack!!.iterator()
        while (iterator.hasNext()) {
            val activity = iterator.next()
            if (activity.javaClass != activityClassToKeep) {
                if (!activity.isFinishing) {
                    activity.finish()
                }
                iterator.remove()
            }
        }
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity?) {
        var activity = activity
        if (activityStack == null) {
            return
        }
        if (activity != null) {
            if (!activity.isFinishing) {
                activity.finish()
            }
            activityStack!!.remove(activity)
            activity = null
        }
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        if (activityStack == null) {
            return
        }
        val size = activityStack!!.size
        for (i in size - 1 downTo 0) {
            val activity = activityStack!![i]
            if (activity != null && !activity.isFinishing) {
                activity.finish()
            }
        }
        activityStack!!.clear()
    }

    /**
     * 根据ActivityName获取堆中Activity实例
     *
     * @param activityName activity的全路径名
     * @return
     */
    fun getActivity(activityName: String?): Activity? {
        if (activityStack != null) {
            val size = activityStack!!.size
            for (i in size - 1 downTo 0) {
                val activity = activityStack!![i]
                if (activity != null && TextUtils.equals(activity.javaClass.name, activityName)) {
                    return activity
                }
            }
        }
        return null
    }

    companion object {
        /**
         * 单一实例
         */
        var instance: ActivityManager? = null
            get() {
                if (field == null) {
                    field = ActivityManager()
                }
                return field
            }
            private set
        private var activityStack: CopyOnWriteArrayList<Activity>? = null
    }
}